package com.zh.note.huawei.onehundred.开心消消乐;

import com.zh.note.huawei.ArraysUtils;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @Author: zhanghuan
 * @date: 2023/8/3 11:10
 * @description: create it
 */
public class Main {
    /**

     * @param args
     */
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // int n = sc.nextInt();
        // int m = sc.nextInt();
        // int count = 0;
        // int[][] target = new int[n][m];
        // for (int i = 0; i < n; i++) {
        //     for (int j = 0; j < m; j++) {
        //         target[i][j] = sc.nextInt();
        //     }
        // }
        int n = 4;
        int m = 4;
        int[][] target = {{1, 1, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 1}, {1, 1, 1, 1}};
        int count = 0;
        for (int i = 0; i < n * m; i++) {
            int x = i / m; //遍历二维数组
            int y = i % m;
            int cur = target[x][y];
            if (cur == 1) {
                count++;
                Deque<int[]> queue = new LinkedList<>();
                // 添加到尾部
                queue.add(new int[]{x, y});
                while (!queue.isEmpty()) {
                    int[] p = queue.removeFirst();
                    for (int[] d : new int[][]{{-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1}}) {
                        int nx = p[0] + d[0];
                        int ny = p[1] + d[1];
                        if (nx >= 0 && nx < n && ny >= 0 && ny < m && target[nx][ny] == 1) {
                            target[nx][ny] = 0;
                            queue.add(new int[]{nx, ny});
                        }
                    }
                }
                ArraysUtils.print2Array(target);
                System.out.println("---------------");
            }
        }
        System.out.println(count);

        /**
         * 二维数组转化为一维
         */

        // int n = 4;
        // int m = 3;
        // for (int i = 0; i < n * m; i++) {
        //     int x = i / m;
        //     int y = i % m;
        //     System.out.println("[" + x + "," + y + "]");
        // }

    }

}
